Conversation
…ementation playbase's nnmCorrect() and NPM's NPmatch() (https://github.com/bigomics/NPM) are the same nearest-pair-matching batch correction algorithm maintained in two places, which had already drifted apart (missing NA-handling, no small-sample/malformed-pairing guards). Replace nnmCorrect()'s body with a thin wrapper delegating to NPM::NPmatch(), keeping the original argument names (use.covariates) so existing playbase callers are unaffected. This also picks up NPM's closed-form replacement for the limma::removeBatchEffect() pairing-effect-removal step, which is mathematically exact but 15-20x faster since it avoids fitting a regression with one factor level per sample. Verified on GSE10846 (n=350): output numerically identical to the previous inline implementation (diff ~4e-13) at both knn=1 and the default knn=2, running in ~2s instead of ~15-31s. nnmCorrect2() is left untouched -- it uses a genuinely different, approximate (SVD-compressed covariate) approach, not superseded by this change. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01KRrp7pzhFakWWzbD9j1i5R
Contributor
|
@ivokwee should this target devel instead of main? |
zitoa
approved these changes
Sep 14, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
nnmCorrect()(inR/pgx-correct.R) andNPM::NPmatch()(https://github.com/bigomics/NPM) are the same nearest-pair-matching batch correction algorithm, maintained as two separately-drifting implementations. ReplacennmCorrect()'s body with a thin wrapper delegating toNPM::NPmatch(), preserving the original argument names (use.covariates) so existing callers throughout playbase (pgx.superBatchCorrect,runBatchCorrectionMethods, etc.) are unaffected.limma::removeBatchEffect()pairing-effect-removal step (mathematically identical, not an approximation), which is much faster because it avoids fitting a regression with one factor level per sample -- the cost that madennmCorrectslow as sample count and/or number of condition groups grew.NPMtoImportsand toRemotes(url::https://github.com/bigomics/NPM/archive/HEAD.zip), following the existing pattern used for other bigomics GitHub packages (playdata,plaid,metaLINCS).nnmCorrect2()is left untouched -- it's a genuinely different, SVD-compressed-covariate approximation, not superseded by this change (see below).Why
nnmCorrect's dominant cost islimma::removeBatchEffect(full.X, batch = full.pairs, design = design), wherefull.pairshas one factor level per original sample. Because the pairing construction always produces a complete, balanced two-way layout (full.pairsxfull.y), the OLS batch coefficient this regression fits collapses to a closed form -- each sample's own mean deviation from the grand mean -- computable with a few vectorised matrix subtractions instead of an n-level factor regression.nnmCorrect2(which tackles the same slowness via per-group KNN + truncated-SVD-compressed covariates): the closed-form fix is faster in every case tested and is exact, whereasnnmCorrect2's output reaches only ~0.85 median gene-wise correlation with the exact result (max deviation > 2x the data's SD on GSE10846) -- consistent with it being harder to control/tending to overcorrect in practice.Speed (output numerically identical, diffs ~1e-12/4e-13 -- floating-point noise, not an approximation)
Real data, GSE10846 (n=350 samples):
Synthetic, scaling with number of condition groups
g(n=400 fixed, knn=1):Synthetic, scaling with sample count
n(g=4 fixed, knn=1):The speedup grows with both
nandg-- exactly the two dimensions along whichnnmCorrectused to get slow -- because the old cost was dominated by ann-level factor regression that the closed form eliminates.Test plan
nnmCorrect()wrapper produces output numerically identical (diffs ~1e-12/4e-13, floating-point noise) to the original inline implementation on GSE10846, at bothknn=1and the defaultknn=2pgx.superBatchCorrectend-to-end withbc="NPM"on a real projectremotes::install_deps()/ CI can resolve the newNPMdependency from theRemotes:URL🤖 Generated with Claude Code
https://claude.ai/code/session_01KRrp7pzhFakWWzbD9j1i5R